package Demo20;

//public class Demo20 {
//    public static int count = 0;
//    public static Object locker = new Object();
//
//    public static void main(String[] args) throws InterruptedException {
//
//        Thread t1 = new Thread(() -> {
//           for(int i = 10000;i > 0;i--) {
//               synchronized (locker) {
//                   count++;
//               }
//
//           }
//        });
//
//        Thread t2 = new Thread(() -> {
//            for(int i = 10000;i > 0;i--) {
//                synchronized (locker) {
//                    count++;
//                }
//            }
//        });
//
//        t1.start();
//        t2.start();
//        t1.join();
//        t2.join();
//
//        System.out.println(count);
//    }
//}

public class Demo20 {
    public static Object locker1 = new Object();
    public static Object locker2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (locker1) {
                System.out.println("t1拿到了locker1");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker2) {

                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (locker2) {
                System.out.println("t2拿到了locker2");

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (locker1) {

                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("完成");
    }
}
